<template>
  <!--会签管理-单据申请-工程项目合同审批弹窗-表单-->
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title"
              style="margin-bottom: 10px;">
              <a-input v-model="model.title" placeholder="请输入标题"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="申请部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="depart_id" style="margin-bottom: 10px;">
              <a-select :disabled="disabled"  placeholder="请选择申请部门" :value="model.departName" @change="handleChange('depart',$event)">
                <a-select-option v-for="(item,index) in departList" :key="index" :value="item">
                      {{item.label}}
                    </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectId"
              style="margin-bottom: 10px;">
              <a-select :disabled="disabled"  placeholder="请选择项目" :value="model.projectName" @change="handleChange('project',$event)">
                <a-select-option v-for="(item,index) in levelOneOptions" :key="index" :value="item">
                      {{item.label}}
                    </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name"
              style="margin-bottom: 10px;">
              <a-input v-model="model.name" placeholder="请输入合同名称"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="合同期限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deadline"
              style="margin-bottom: 10px;">
              <div style="display: flex;align-items: center;">
                <a-input style="width: calc(100% - 40px);" v-model="model.deadline" placeholder="请输入合同期限"></a-input><div style="width: 40px;">个月</div>
              </div>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同份数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number"
              style="margin-bottom: 10px;">
              <a-input v-model="model.number" placeholder="请输入合同份数"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="对方单位名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="other"
              style="margin-bottom: 10px;">
              <a-input v-model="model.other" placeholder="请输入对方单位名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="money"
              style="margin-bottom: 10px;">
              <a-input v-model="model.money" placeholder="请输入合同金额"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="对方单位地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="otherAddress"
              style="margin-bottom: 10px;">
              <a-input v-model="model.otherAddress" placeholder="请输入对方单位地址"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractNumber"
              style="margin-bottom: 10px;">
              <a-input v-model="model.contractNumber" placeholder="请输入合同编号"></a-input>
            </a-form-model-item>
          </a-col>

        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="对方单位电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="otherPhone"
              style="margin-bottom: 10px;">
              <a-input v-model="model.otherPhone" placeholder="请输入对方单位电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime"
              style="margin-bottom: 10px;">
              <a-date-picker placeholder="请选择开始日期" format="YYYY-MM-DD" v-model="model.startTime"
                style="width:100%;" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="合同备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="remark"
              style="margin-bottom: 10px;">
              <textarea v-model="model.remark" cols="100" rows="3" style="width: 119%;"></textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="合同协议附件资料" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment"
              style="margin-bottom: 10px;">
              <j-upload v-model="model.attachment"></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="pageType === 'add'">
            <div style="display: flex;justify-content: space-between;align-items: center;margin-right: 40px;">
              <div style="line-height: 20px;color: red;">1.请附会签完成的询价单；<br />2.请附对方营业执照、资质证书等相关材料。</div>
              <a-button @click="downfile" type="primary">下载合同模板</a-button>
            </div>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import {
    httpAction,
    getAction
  } from '@/api/manage'
  import {
    validateDuplicateValue
  } from '@/utils/util'

  export default {
    name: 'WtBrandForm',
    components: {},
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false,
      },

    },
    data() {
      return {
        model: {

        },
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 6
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 14
          },
        },
        labelCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 3
          },
        },
        wrapperCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },
        confirmLoading: false,
        validatorRules: { },
        levelOneOptions: [],
        departList: [], //部门列表
        treeData: [],
        url: {
          add: '/countersign/wtCountersignProjectContract/add',
          edit: '/countersign/wtCountersignProjectContract/edit',
          anew:'/countersign/wtCountersignProjectContract/editSubmit',
          queryById: '/wtBrand/queryById',
        },
        templateId:'',
        pageType: 'edit', //edit-编辑 anew-重新提交
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      },
    },
    created() {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      if(!!window.localStorage.getItem("pro__USER_DEPARTLIST")){
        let departList = JSON.parse(window.localStorage.getItem("pro__USER_DEPARTLIST")).value;
        if(departList && departList.length>0){
          this.departList = departList.map((ite)=>({
            ...ite,
            value:ite.id,
            label:ite.departName
          }))
        }
      }
    },
    methods: {
      handleChange(type,item){
        console.log(1,type,item);
        if(type === 'project'){
          this.model.projectName = item.label;
          this.model.projectId = item.value;
        }else if(type === 'depart'){
          this.model.departName = item.label;
          this.model.depart_id = item.value;
        }
      },
      loadTree(record) {
        var that = this;
        getAction(`/sys/sysDepart/getDepartByOrgType?orgType=1`).then((res) => {
          if (res.success) {
            this.treeData = []
            let treeList = res.result
            for (let a = 0; a < treeList.length; a++) {
              let temp = treeList[a]
              temp.isLeaf = temp.leaf
              this.treeData.push(temp)
            }
        
            this.levelOneOptions = [...this.treeData].map((v) => {
              return {
                ...v,
                value: v.id,
                label: v.departName,
              }
            })
            getAction(`/countersign/wtCountersignProjectContract/queryByCountersignId?countersignId=${record.id}`).then(
              (res) => {
                if (res.success) {
                  this.model = res.result;
                  this.list = res.result.list;
                  if(this.levelOneOptions && this.levelOneOptions.length>0){
                    let info = this.levelOneOptions.find((item) => item.departName == this.model.projectName);
                    this.model.projectId = info.id;
                  }
                  if(this.departList && this.departList.length>0){
                    let info2 = this.departList.find((item) => item.departName == this.model.departName);
                    this.model.depart_id = info2.id;
                  }
                  this.$forceUpdate();
                }
              }
            )
          }
        })
      },
      add(record) {
        console.log(22, record)
        this.templateId = record.id;
        this.edit(this.modelDefault)
      },
      edit(record, type) {
        console.log(2,record,type)
        this.model = Object.assign({}, record)
        console.log(123, record, type)
        if (!!type) {
          this.pageType = type;
        }
        this.visible = true
        this.loadTree(record)
        this.templateId = record.id
      },
      save() {
        const that = this
        // 触发表单验证
        this.$refs.form.validate((valid) => {
          if (valid) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            that.model.status = '0'
            httpAction('/countersign/wtCountersignPaymentRequest/add', this.model, 'POST')
              .then((res) => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.$emit('ok')
                } else {
                  that.$message.warning(res.message)
                  that.$emit('error')
                }
              })
              .finally(() => {
                that.confirmLoading = false
              })
          }
        })
      },
      downfile() {
        this.$emit('待更新')
      },
      submitForm(record) {
        let that = this;
        if (!this.model.attachment) {
          that.$message.warning('附件资料不能为空,请上传')
        } else {
          // 触发表单验证
          this.$refs.form.validate((valid) => {
            if (valid) {
              that.confirmLoading = true
              this.model.status = record;
              let httpurl = ''
              if (that.pageType === 'edit') {
                httpurl = that.url.edit
              } else if(that.pageType === 'anew'){
                httpurl = that.url.anew
              }
              httpAction(httpurl, this.model, 'POST')
                .then((res) => {
                  if (res.success) {
                    that.$message.success(res.message)
                    that.$emit('ok')
                  } else {
                    that.$message.warning(res.message)
                    that.$emit('error')
                  }
                })
                .finally(() => {
                  that.confirmLoading = false
                })
            }
          })
        }
        
      },
    },
  }
</script>